package ui.uea;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelListener;
import javax.swing.table.JTableHeader;

import ui.component.FontPack;
import ui.login_ui.Mouse;
import vo.ModelVO;

@SuppressWarnings("serial")
public class FrameworkPanel extends JPanel {
			private UEAPicturePath path = new UEAPicturePath();
			private String bgPath;
//[x=74,y=116][x=157,y=116][x=213,y=160][x=213,y=284]

//	[x=25,y=119][x=212,y=201]
			private Rectangle addModelRect = new Rectangle(74,119,157 - 74,201 - 160);
			private JButton addModelButton;
			private MouseListener addModelListener;
//	[x=23,y=242][x=211,y=322]
			private Rectangle addSubModelRect = new Rectangle(74,242,157 - 74,322 - 284);
			private JButton addSubModelButton;
			private MouseListener addSubModelListener;
//	[x=25,y=357][x=214,y=430]
			@SuppressWarnings("unused")
			private Rectangle addPreCourseRect = new Rectangle(74,357,157 - 74,322 - 242);
			private JButton addPreCourseButton;
			private MouseListener addPreCourseListener;
			private JButton deletePreCourseButton;
			private MouseListener deletePreCourseListener;
//			[x=15,y=119][x=223,y=540]
			@SuppressWarnings("unused")
			private Rectangle	labelPanelRect = new Rectangle(67,119,104,540 - 119);
			@SuppressWarnings("unused")
			private JPanel buttonPanel;
//			x=260,y=91][x=945,y=538]
			private Rectangle frameworkRect = new Rectangle(260,91, 945 - 260, 430 - 91);
			private JScrollPane scrollPane;
			private FrameworkTable framework;
			private JTable frameworkTable;
			private TableModelListener frameworkTableListener;
			private List<ModelVO> models;
			public FrameworkPanel(String bgpath){
				this.bgPath = bgpath;
				 framework  = new FrameworkTable();
				 frameworkTable = framework.getFrameworkTable();
				 
				this.setLayout(null);
				
				initScrollPane();
				this.add(scrollPane);
				
				initButton();
//				this.add(buttonPanel);
				this.add(this.addModelButton);
				this.add(this.addSubModelButton);
				
				
				
				this.addMouseListener(new Mouse());

			}
			public MouseListener getAddModelListener() {
				return addModelListener;
			}
			public void setAddModelListener(MouseListener addModelListener) {
				this.addModelButton.addMouseListener(addModelListener);
			}
			public MouseListener getAddSubModelListener() {
				return addSubModelListener;
			}
			public void setAddSubModelListener(MouseListener addSubModelListener) {
				this.addSubModelButton.addMouseListener(addSubModelListener);
			}
			public MouseListener getAddPreCourseListener() {
				return addPreCourseListener;
			}
			public void setAddPreCourseListener(MouseListener addPreCourseListener) {
				this.addPreCourseButton.addMouseListener(addPreCourseListener);
			}
			public MouseListener getDeletePreCourseListener() {
				return deletePreCourseListener;
			}
			public void setDeletePreCourseListener(MouseListener deletePreCourseListener) {
				this.deletePreCourseButton.addMouseListener(deletePreCourseListener);
			}
			public TableModelListener getFrameworkTableListener() {
				return frameworkTableListener;
			}
			public void setFrameworkTableListener(TableModelListener frameworkTableListener) {
				this.frameworkTable.getModel().addTableModelListener(frameworkTableListener);
			}
			public List<ModelVO> getModels() {
				return models;
			}
			public JTable getFrameworkTable() {
				return frameworkTable;
			}
			public void fillFramework(List<ModelVO> models){
				framework.fillFramework(models);
			}

			private void initScrollPane(){
				setTableHeader();
				
				scrollPane = new JScrollPane(frameworkTable);
				scrollPane.setBounds(frameworkRect);
				scrollPane.getViewport().setOpaque(false);
				scrollPane.setOpaque(false);
				scrollPane.setBorder(new EmptyBorder(0,0,0,0));
			}
			private void setTableHeader(){
			JTableHeader header = frameworkTable.getTableHeader();
			header.setFont(FontPack.getModelFont(15));
			header.setBackground(Color.WHITE);
			header.setOpaque(false);
			header.setForeground(new Color(0,150,219));
			header.setBorder(new EmptyBorder(0,0,0,0));
		}
			private void initButton(){
//				buttonPanel = new JPanel();
//				buttonPanel.setBounds(labelPanelRect);
//				buttonPanel.setLayout(new GridLayout(4,1,5,5));
//				
//				int h = 10;
//				
				addModelButton = new JButton();
				addModelButton.setBounds(addModelRect);
				addModelButton.setBackground(Color.WHITE);
				addModelButton.setBorder(new EmptyBorder(0,0,0,0));
				setImage(addModelButton,path.getAddModelButton());
//				buttonPanel.add(addModelButton);
				
				addSubModelButton = new JButton();
				addSubModelButton.setBounds(addSubModelRect);
				addSubModelButton.setBackground(Color.WHITE);
				addSubModelButton.setBorder(new EmptyBorder(0,0,0,0));
				setImage(addSubModelButton,path.getAddSubModelButton());
//				buttonPanel.add(addSubModelButton);
				
//				addPreCourseButton = new JButton();
//				addPreCourseButton.setSize(buttonPanel.getWidth(), h);
//				setImage(addPreCourseButton,path.getAddPreCourseButton());
//				buttonPanel.add(addPreCourseButton);
//				
//				deletePreCourseButton = new JButton();
//				deletePreCourseButton.setSize(buttonPanel.getWidth(), h);
//				setImage(deletePreCourseButton,path.getDeletePreCourseButton());
//				buttonPanel.add(deletePreCourseButton);
			}
			private void setImage(JButton button, String path){
				ImageIcon icon = new ImageIcon(path);
				button.setIcon(new ImageIcon(icon.getImage().getScaledInstance(button.getWidth(), button.getHeight(), Image.SCALE_DEFAULT)));
				button.setOpaque(false);
			}

			@Override 
			protected void paintComponent(Graphics g){
				super.paintComponent(g);
				BufferedImage im = null;
				try {
					 im = ImageIO.read(new File(bgPath));
				} catch (IOException e) {
					e.printStackTrace();
				}
				g.drawImage(im, 0, 0,this.getWidth(),this.getHeight(), null);
			}
	
	
	
}
